import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.SwingConstants;

public class UnoNew_UI extends JDialog
{
	private static final long serialVersionUID = -5240194517533264378L;
	private String[] PLAYERS_SELECT = { "Select Number of Players",
			"2 Players", "3 Players", "4 Players", "5 Players" };
	private int count = 0;
	protected static int numPlayer = 0;

	private static JPanel contentPanel = new JPanel();
	private static JComboBox<String> dropDown = new JComboBox<String>();
	private static JPanel btnPane = new JPanel();
	private static JButton btnOK = new JButton("OK");
	private static JButton btnCancel = new JButton("CANCEL");	

	private Component frame_1;

	/**
	 * Launch UnoNew_UI
	 */
	public static void launch()
	{
		try
		{
			UnoNew_UI dialog = new UnoNew_UI();

			dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public UnoNew_UI()
	{
		super();

		count = 0;
		numPlayer = 0;

		Uno_UI.setAttributesDialog(this, "New Game: Number of Players");
		Uno_UI.setUIManager();
		Uno_UI.addWindowListenerClosing(this);

		getContentPane().setLayout(new BorderLayout());
		contentPanel.setForeground(Uno_UI.WHITE);
		contentPanel.setBackground(Uno_UI.BLACK);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		createLabels();
		createDropDown();
		createButtons();
	}

	private static void createLabels()
	{
		JLabel lblCreateGame = new JLabel("Create Game");
		lblCreateGame.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCreateGame.setForeground(Uno_UI.WHITE);
		lblCreateGame.setFont(Uno_UI.CONSOLAS18);
		lblCreateGame.setBounds(10, 11, 111, 39);
		contentPanel.add(lblCreateGame);

		JLabel lblNumberOfPlayers = new JLabel("Number of Players");
		lblNumberOfPlayers.setForeground(Uno_UI.WHITE);
		lblNumberOfPlayers.setBackground(Uno_UI.WHITE);
		lblNumberOfPlayers.setBounds(20, 104, 122, 14);
		contentPanel.add(lblNumberOfPlayers);
	}

	private void createDropDown()
	{
		dropDown = new JComboBox<String>();
		dropDown.setBackground(Color.GRAY);
		dropDown.setForeground(Uno_UI.BLACK);
		dropDown.setBounds(142, 101, 203, 20);

		addActionListenerDropDown();

		contentPanel.add(dropDown);

		for (int i = 0; i < PLAYERS_SELECT.length; i++)
			dropDown.addItem(PLAYERS_SELECT[count++]);
	}

	private void addActionListenerDropDown()
	{
		dropDown.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				JComboBox<?> comboBox = (JComboBox<?>) e.getSource();
				String player = (String) comboBox.getSelectedItem();

				if (player.equals(PLAYERS_SELECT[0]))
				{
					setNumPlayer(0);
				}
				if (player.equals(PLAYERS_SELECT[1]))
				{
					setNumPlayer(2);
				}
				if (player.equals(PLAYERS_SELECT[2]))
				{
					setNumPlayer(3);
				}
				if (player.equals(PLAYERS_SELECT[3]))
				{
					setNumPlayer(4);
				}
				if (player.equals(PLAYERS_SELECT[4]))
				{
					setNumPlayer(5);
				}
			}
		});
	}

	private void createButtons()
	{
		btnPane = new JPanel();
		btnPane.setBackground(Uno_UI.BLACK);
		btnPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		getContentPane().add(btnPane, BorderLayout.SOUTH);
		
		createButtonOK();
		createButtonCancel();
	}
	
	private void createButtonOK()
	{
		btnOK = new JButton("OK");
		addActionListenerButtonOK();
		btnOK.setActionCommand("OK");
		btnPane.add(btnOK);
		getRootPane().setDefaultButton(btnOK);
	}
	
	private void addActionListenerButtonOK()
	{
		btnOK.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent okClicked)
			{
				try
				{
					if (numPlayer == 0)
						throw new Exception();
					else
					{
						setVisible(false);
						UnoPlayers_UI.launch();
					}

				} catch (Exception e2)
				{
					e2.printStackTrace();
					JOptionPane.showMessageDialog(frame_1,
							"Please select at least 2 players",
							"Warning!", JOptionPane.WARNING_MESSAGE);
				}
			}
		});
	}

	private void createButtonCancel()
	{
		btnCancel = new JButton("Cancel");
		addActionListenerButtonCancel();
		btnCancel.setActionCommand("Cancel");
		btnPane.add(btnCancel);
	}
	
	private void addActionListenerButtonCancel()
	{
		btnCancel.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent cancelClicked)
			{
				Uno_UI.confirmExit();
			}
		});
	}

	protected static int getNumPlayer()
	{
		return numPlayer;
	}

	public void setNumPlayer(int numPlayer)
	{
		UnoNew_UI.numPlayer = numPlayer;
	}
}

